如果在单击按钮之前省略了不同单选按钮组中的任何单选按钮,则发出警报

时间:2018-04-21 10:14:56

标签: javascript jquery html radio-button

我有这个jquery,它从不同的单选按钮组中获取单选按钮输入。如果在点击按钮之前检查了这些中的任何一个或没有任何一个,我想要提醒用户。 这是jquery:

 $("#query").click(function() {  

  $("#loading-div-background").show();
  $("#img").hide()
  $("#container").hide();

  var category = $('input:radio[name=category]:checked').val();
  var time = $('input:radio[name=time]:checked').val()
  var plotchoice = $('input:radio[name=plotchoice]:checked').val()
  var qu = {"category":category,"time":time,"plotchoice":plotchoice}
  $.ajax({
    type: "POST",
    async:true,
    contentType: "application/json; charset=utf-8",
    url: "/query",
    data: JSON.stringify(qu),
    success: function (data) {     
     var graph = $("#container");
     graph.html(data);   
     $("#loading-div-background").hide();      
     $("#container").show();
   },
   dataType: "html"
 });
});

这是html:

 <label style="font-family: Comic Sans MS;">Choose a category:</label><br/>

    <!--set 1-->

<input type="radio" name="category" value="c1"><label style="margin-left: 10px;font-family: Comic Sans MS;; vertical-align: middle;">Status</label> </input>
&nbsp;
<input type="radio" name="category" value="c2"><label style="margin-left: 10px;font-family: Comic Sans MS;; vertical-align: middle;">Trend</label> </input><br>
<!-- sset 2-->
<br/>

<label style="font-family: Comic Sans MS;">Choose a time frame:</label><br/>
<input type="radio" name="time" value="t1"><label style="margin-left: 10px;font-family: Comic Sans MS;; vertical-align: middle;"> Recently</label> </input>
&nbsp;
<input type="radio" name="time" value="t2"><label style="margin-left: 10px;font-family: Comic Sans MS;; vertical-align: middle;"> Last 1 Week</label> </input>
&nbsp;
<input type="radio" name="time" value="t2"><label style="margin-left: 10px;font-family: Comic Sans MS;; vertical-align: middle;"> Last 1 Month</label> </input>
&nbsp;
<input type="radio" name="time" value="t2"><label style="margin-left: 10px;font-family: Comic Sans MS;; vertical-align: middle;"> Last 1 Year </label></input>
&nbsp;
<br/>
<br/>
<!--set 3-->

<label style="margin-left: 10px;font-family: Comic Sans MS;">Choose a plot type:</label><br/>
<input type="radio" name="plotchoice" value="p1"><label style="margin-left: 10px;font-family: Comic Sans MS;;  vertical-align: middle;"> Bar </label></input>
&nbsp;
<input type="radio" name="plotchoice" value="p2"><label style="margin-left: 10px;font-family: Comic Sans MS;;  vertical-align: middle;"> Line </label></input><br>
<br/>
<button type="button" id="query" style="font-family: Comic Sans MS;;  vertical-align: middle;">View Plot</button>

正如您所看到的那样,我已经标记了3组单选按钮,在按钮单击之前需要从中选择一组桌面按钮。如果遗漏任何一个或者没有选择,我想显示警告。  注意:jquery新手

感谢任何帮助。谢谢!

1 个答案:

答案 0 :(得分:0)

可以检查这样的单选按钮的状态:

$cut = strlen($item["description"]) < 20 ? $item["description"] : substr($item["description"], 0, 20) . "...";
$art = $cut;
echo $art;