jQuery:更改隐藏的输入字段的值 [英] JQuery: Change value of hidden input field
问题描述
我正在忙于设置隐藏输入的值.
I'm having a hella time setting the value of a hidden input.
我想将选项标签之间的HTML传递给隐藏的字段,然后运行它,它将来自wordpress'wp_list_dropdowns()
的页面标题.我已经很好地返回了文本,并且在我的change事件上它正确地添加了一些CSS(显然不需要隐藏字段,但是我试图确定发生故障的地方).如果将隐藏的输入更改为文本输入,则可以使用.我已经在SO上的多个地方看到了这是可能的(更改隐藏的输入的值),但是现在有些事情阻止了我,但我看不到答案.
I want to pass the HTML from between the option tags to the hidden field- end run it will the page title from wordpress' wp_list_dropdowns()
. I've got it returning the text just fine, and on my change event it correctly adds some css (obviously unneeded on a hidden field, but I was trying to determine where things are breaking down). Works if I change the hidden input to a text input. I've seen in several places on SO that this is possible, (changing the value of a hidden input that is), but something is holding me up now and I can't see the answer.
这是 JSFiddle :
JavaScript:
JavaScript:
$(".selector").change(function() {
var $value = $(this).val();
var $title = $(this).children('option[value='+$value+']').html();
alert($title);
$('input#bacon').val($title).css('border','3px solid blue');
});
HTML:
<select class="selector" name="testselect">
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
</select>
</h3>
<input id="bacon" type="hidden" class="bacon" value="" name="testinput">
推荐答案
您的jQuery代码可以完美运行. 隐藏字段 正在更新.
Your jQuery code works perfectly. The hidden field is being updated.
这篇关于jQuery:更改隐藏的输入字段的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!