0

我正在尝试设置一个复选框以从 if 语句中检查,但它似乎不想工作......

这是 HTML:

<input type="checkbox" id="aircon" name="aircon" onchange="QuoteRefresh();" class="toggle"  />

这是在我的 .js 文件中:

$('#aircon').prop('checked', true);

我在控制台中看不到任何错误。

任何人都可以对此有所了解。

它在 AJAX 成功响应中:

if (SelectedLevel == "Gold") {


  $('#aircon').prop('checked', true);

}

谁能帮我??

这就是我在控制台中得到的:

[input#aircon.toggle, context: document, selector: "#aircon", constructor: function, init:      function, selector: ""…]
0: input#aircon.toggle
context: document
length: 1
selector: "#aircon"
__proto__: Object[0]
4

1 回答 1

0

The code you posted should work, maybe your QuoteRefresh(); method is returning false, or maybe your code handling the checked propertery isn't attached to the appropriate event that you want.

Here's a JSFiddle of the prop method working.

edit (with information gathered from your initial comment on this answer)

If I'm understanding right, you're using the bootstrap toggle buttons component (which is represented with radio button values), and you're trying to change the actively toggled button with .prop('checked', true); but your components visual style isn't changing. This is because changing the checked property on a radio button from true to false or vice versa doesn't trigger any javascript events, which bootstrap relies on in order to update the visual state of the radio buttons.

于 2013-07-11T21:24:30.037 回答