设置隐藏输入的值 [英] Setting the value of a hidden input

查看:96
本文介绍了设置隐藏输入的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个想法:我通过常规的Javascript或jQuery设置type =hidden的输入值。



问题:jQuery和document都没有。 getElementById会找到隐藏的输入,即使我确定选择器是正确的,并且没有冲突的元素。



代码:
我可以'因为当我看着它时,它充满了相当复杂的PHP,让我感到困惑。



以下是javascript:

  $(#+ input.id.substr(0,2)+_budget_hidden)。val(budg_total); 

注意:选择器没有问题,输入是我用来引用隐藏的元素。



这里是HTML:

 < input type =hiddenname =s<?= $ step_counter?> _budget_hidden
id =s<?= $ step_counter?> _budget_hiddenvalue = 0/>

这段代码与上下文无关,但它更像是Javascript的一个常见问题,而不是语法错误。在 $(#+ input.id.substr(0,2))中找到

解决方案 +_budget_hidden)。val(budg_total); 你在隐藏的id中的第一个下划线之前取两个字符。然而,您的隐藏ID只有一个字符''''''''''''''''''$ / $>在问题编辑之前隐藏<?=?>



body onload事件?
EX:

  $(document).ready(function(){
$(# + input.id.substr(0,2)+_budget_hidden)。bind(keyPressed,function(){
$(#+ input.id.substr(0,2)+_budget_hidden ).val(budg_total);
}
});


The idea: I'm setting the value of an input with type="hidden" via regular Javascript or jQuery.

The issue: neither jQuery nor document.getElementById will find the hidden input, even though I'm absolutely sure the selector is correct and there are no conflicting elements.

The code: I can't really post much of it, because it's full of rather complicated PHP that confuses me when I just look at it.

Here's the javascript:

$("#" + input.id.substr(0,2) + "_budget_hidden").val(budg_total);

Note: there's nothing wrong with the selector, and the "input" is a different element that I'm using to reference the hidden.

Here's the HTML:

<input type="hidden" name="s<?=$step_counter?>_budget_hidden" 
       id="s<?=$step_counter?>_budget_hidden" value="0" />

The code is kind of out of context, but it's more of a general problem with Javascript than a syntactical error. Thoughts?

解决方案

In $("#" + input.id.substr(0,2) + "_budget_hidden").val(budg_total); you take two chars before the first underscore in your hidden id. However your hidden id have only one char 's'

EDIT

Ok the <?= ?> was hidden before the question edit.

Do you call your script after the body onload event? EX:

$(document).ready(function(){
    $("#" + input.id.substr(0,2) + "_budget_hidden").bind("keyPressed",function(){
        $("#" + input.id.substr(0,2) + "_budget_hidden").val(budg_total);
     }
});

这篇关于设置隐藏输入的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆