jQuery - 无法更新隐藏的输入属性

时间:2012-04-16 14:23:52

标签: jquery field

我正在尝试构建一个表单来禁用菜单中用户选择所确定的隐藏字段。在开始时,我可以非常轻松地禁用所有字段,并且当用户选择其他选项时,我可以重新启用特定字段。但是,当用户点击“更改位置”时,我需要能够重新禁用所有字段,但我根本无法使其正常工作!

到目前为止,这是我的代码......我的代码背后的目的可能并不完全清楚,因为为了清晰起见,它进行了大量编辑,但发生了各种其他不相关的事情。我只想说,这就是我需要做的事情:)

jQuery的:

$('input[name="hosted_button_id"]').attr("disabled",true);

$('#changelocation').click(function(){
    $('input[name="hosted_button_id"]').attr("disabled",true);
});


$('.deliveryselection').change(function(){    
    var deliveryLocation = $(this).val();
    $('input[title="' + deliveryLocation + '"]').removeAttr("disabled");
});

HTML:

<select class="deliveryselection">
    <option>Please select</option>
    <option value="UK">UK</option>
    <option value="Europe">Europe</option>
    <option value="North America">North America</option>
    <option value="Rest of World">Rest of World</option>
</select>
<a href="javascript:void(0)">Change location</a>
<form>
    <input type="hidden" name="cmd" value="_s-xclick">
    <input type="button" name="hosted_button_id" title="UK" value="UK"><br />
    <input type="button" name="hosted_button_id" title="Europe" value="Europe"><br />
    <input type="button" name="hosted_button_id" title="North America" value="North America"><br />
    <input type="button" name="hosted_button_id" title="Rest of World" value="Rest of World"><br /><br />
    <input type="submit" name="submit">
</form>
​

我已经在这里设置了一个jsfiddle:http://jsfiddle.net/solomon/rS7nV/1/

修改 我想我已经发现了这个问题。在我的实际代码中,我一直在处理隐藏的字段,而不是按钮字段,所以我无法直接看到我的劳动成果 - 我一直在使用Firebug来确定隐藏字段的禁用条件。它只是通过将它们转换为按钮,同时使用jsfiddle,我发现我的代码工作正常(除了我在删除ID时发生的小学生错误...感谢您的帮助!)。事实证明Firebug没有更新它的报告,并且仍然告诉我,当它们没有时,字段被禁用 - 尽管事实上我的代码工作得很好!这有点令人沮丧 - 我的整个下午浪费在Firebug上! :)

没关系 - 现在全部修好了:)

3 个答案:

答案 0 :(得分:3)

问题是您没有ID为changelocation的元素。添加ID,没关系。

http://jsfiddle.net/rS7nV/4/

答案 1 :(得分:0)

如果您为包含隐藏字段的表单提供ID,则可以轻松禁用所有隐藏字段:

$("#hiddenFormId input").attr("disabled", true);

答案 2 :(得分:0)

在div中声明一个类,使用jQuery调用它,如下所示

jQuery(“。declariedclassname”)。attr(“disabled”,true);

相关问题