【问题标题】:Alert when checkbox is checked in Javascript / Jquery automatically自动在Javascript / Jquery中选中复选框时发出警报
【发布时间】:2020-09-03 11:37:17
【问题描述】:

试图提醒选中的单选按钮的 id。但它没有警报。

// Reference the form - see HTMLFormControlsCollection
var chxGrp = document.forms.checkGroup;

chxGrp.addEventListener('change', message);

/* The Event Object is passed through, you might've seen it as 
|| "e" or "evt". How it's labelled doesn't matter because Event
|| Object is always available when you deal with the DOM.
*/
function message(event) {

  // if the node clicked is a checkbox see if it's...
  if (event.target.type === 'checkbox') {

    // ... checked then...
    if (event.target.checked) {

      wrong_id = event.target.attr('id');
      alert(wrong_id);
    } else {}
  }
  return false;
}
<form id='checkGroup'>

  Question 1
  <div class="question_container">
    <div class="question_div1">
      <div class="question_div2">
        <p>Some persons can do piece of work in 12 days. Twice the number of such persons will do half of that work in, how many days.</p>

        <p>&nbsp;</p>
        <input type="hidden" name="qid1" value="244">
        <div class="option_div">
          <input type="radio" name="radio1" id="checka1" value="A">
          <label for="checka1">
            <p>1</p>
          </label>
        </div>
        <div class="option_div">
          <input type="radio" name="radio1" id="checkb1" value="B">
          <label for="checkb1">
            <p>2</p>
          </label>
        </div>
        <div class="option_div">
          <input type="radio" name="radio1" id="checkc1" value="C">
          <label for="checkc1">
            <p>3</p>
          </label>
        </div>
        <div class="option_div">
          <input type="radio" name="radio1" id="checkd1" value="D">
          <label for="checkd1">
            <p>4</p>
          </label>
        </div>
        <div class="option_div">
          <input type="radio" name="radio1" id="checke1" value="E">
          <label for="checke1">
            <p>None of these</p>
          </label>
        </div>
      </div>
    </div>
  </div>

  Question 2
  <div class="question_container">
    <div class="question_div1">
      <div class="question_div2">
        <p>In a dairy farm, 40 cows eat 40 bags of husk in 40 days. In how many days one cow will eat one bag of husk.</p>
        <input type="hidden" name="qid2" value="245">
        <div class="option_div">
          <input type="radio" name="radio2" id="checka2" value="A">
          <label for="checka2">
            <p>&nbsp;44</p>
          </label>
        </div>
        <div class="option_div">
          <input type="radio" name="radio2" id="checkb2" value="B">
          <label for="checkb2">
            <p>45</p>
          </label>
        </div>
        <div class="option_div">
          <input type="radio" name="radio2" id="checkc2" value="C">
          <label for="checkc2">
            <p>48</p>
          </label>
        </div>
        <div class="option_div">
          <input type="radio" name="radio2" id="checkd2" value="D">
          <label for="checkd2">
            <p>40</p>
          </label>
        </div>
        <div class="option_div">
          <input type="radio" name="radio2" id="checke2" value="E">
          <label for="checke2">
            <p>None of these</p>
          </label>
        </div>
      </div>
    </div>
  </div>
</form>

试图提醒选中的单选按钮的 id。但它没有警报。

试图提醒选中的单选按钮的 id。但它没有警报。

试图提醒选中的单选按钮的 id。但它没有警报。

【问题讨论】:

  • attr() 是 jQuery 方法,不是原生 dom 元素方法

标签: javascript html jquery alert radio


【解决方案1】:

您的代码中有 2 个错误:您检查 target.type==="checkbox",而您应该检查 target.type=="radio",并且您必须使用 getAttribute() 而不是 attr()

// Reference the form - see HTMLFormControlsCollection
var chxGrp = document.forms.checkGroup;

chxGrp.addEventListener('change', message);

function message(event) {

  // if the node clicked is a checkbox see if it's...
  if (event.target.type === 'radio') {

    // ... checked then...
    if (event.target.checked) {

      wrong_id = event.target.getAttribute('id');
      alert(wrong_id);
    } else {}
  }
  return false;
}
<form id='checkGroup'>

  Question 1
  <div class="question_container">
    <div class="question_div1">
      <div class="question_div2">
        <p>Some persons can do piece of work in 12 days. Twice the number of such persons will do half of that work in, how many days.</p>

        <p>&nbsp;</p>
        <input type="hidden" name="qid1" value="244">
        <div class="option_div">
          <input type="radio" name="radio1" id="checka1" value="A">
          <label for="checka1">
            <p>1</p>
          </label>
        </div>
        <div class="option_div">
          <input type="radio" name="radio1" id="checkb1" value="B">
          <label for="checkb1">
            <p>2</p>
          </label>
        </div>
        <div class="option_div">
          <input type="radio" name="radio1" id="checkc1" value="C">
          <label for="checkc1">
            <p>3</p>
          </label>
        </div>
        <div class="option_div">
          <input type="radio" name="radio1" id="checkd1" value="D">
          <label for="checkd1">
            <p>4</p>
          </label>
        </div>
        <div class="option_div">
          <input type="radio" name="radio1" id="checke1" value="E">
          <label for="checke1">
            <p>None of these</p>
          </label>
        </div>
      </div>
    </div>
  </div>

  Question 2
  <div class="question_container">
    <div class="question_div1">
      <div class="question_div2">
        <p>In a dairy farm, 40 cows eat 40 bags of husk in 40 days. In how many days one cow will eat one bag of husk.</p>
        <input type="hidden" name="qid2" value="245">
        <div class="option_div">
          <input type="radio" name="radio2" id="checka2" value="A">
          <label for="checka2">
            <p>&nbsp;44</p>
          </label>
        </div>
        <div class="option_div">
          <input type="radio" name="radio2" id="checkb2" value="B">
          <label for="checkb2">
            <p>45</p>
          </label>
        </div>
        <div class="option_div">
          <input type="radio" name="radio2" id="checkc2" value="C">
          <label for="checkc2">
            <p>48</p>
          </label>
        </div>
        <div class="option_div">
          <input type="radio" name="radio2" id="checkd2" value="D">
          <label for="checkd2">
            <p>40</p>
          </label>
        </div>
        <div class="option_div">
          <input type="radio" name="radio2" id="checke2" value="E">
          <label for="checke2">
            <p>None of these</p>
          </label>
        </div>
      </div>
    </div>
  </div>
</form>

如果你之前转换event.target,你可以使用jQuery的attr()函数:$(event.target).attr("id"),但是由于你的完整代码是在javascript中,所以这里不需要使用jQuery。

【讨论】:

  • 谢谢,它有帮助。在 JQuery 中实现完整的代码是否会使代码又快又小?
  • @vipejo 虽然差别不大,但使用 javascript 代替 jQuery 应该更快。 jQuery 只是一个 javascript 库,需要被解释,所以使用纯 javascript 更快。
  • @vipejo 它会小一点,你可以在这里看到:jsfiddle.net/py9mux8L
猜你喜欢
  • 2010-11-01
  • 1970-01-01
  • 2019-08-24
  • 1970-01-01
  • 2018-05-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多