如何检查值是否正确选择(或不)通过JavaScript?
问题描述:
我想从一个单选按钮的形式添加到我的数据库的值,但我的JavaScript不会返回任何错误,它不工作。我认为我的选择器可能不工作,但我该如何检查?我的功能有什么问题?如何检查值是否正确选择(或不)通过JavaScript?
JS
<script type="text/javascript" >
function addScore() {
$("#submitscore").click(function()
{
var show_id = $('#show_id').val();
var user_id = $('#user_id').val();
var score = $('input[name=tvshowrating]:checked').val();
if(score=='')
{
alert('PleaseEnter A Score');
}
else
{
$("#flash").show();
$("#flash").fadeIn(400).html('<img src="ajax-loader.gif" />Loading Score...');
$.ajax({
type: "POST",
url: "showscoreajax.php",
data:{
"show_id" : show_id,
"user_id" : user_id,
"score" : score //we are passing the name value in URL
},
cache: false,
success: function(html){
$("#flash").html('Added');
}
});
}return false;
});
};
</script>
HTML
<form id="form3B">
<div class="your-score">
<div class="">Your Score</div>
<div id="flash"></div>
<input class="hover-star" type="radio" name="tvshowrating" value="1" title="1"/>
<input class="hover-star" type="radio" name="tvshowrating" value="2" title="2"/>
<input class="hover-star" type="radio" name="tvshowrating" value="3" title="3"/>
<input class="hover-star" type="radio" name="tvshowrating" value="4" title="4"/>
<input class="hover-star" type="radio" name="tvshowrating" value="5" title="5"/>
<input class="hover-star" type="radio" name="tvshowrating" value="6" title="6"/>
<input class="hover-star" type="radio" name="tvshowrating" value="7" title="7"/>
<input class="hover-star" type="radio" name="tvshowrating" value="8" title="8"/>
<input class="hover-star" type="radio" name="tvshowrating" value="9" title="9"/>
<input class="hover-star" type="radio" name="tvshowrating" value="10" title="10"/>
<input type="hidden" id="show_id" value="<?php echo $row[0]; ?>" />
<input type="hidden" id="user_id" value="<?php echo $user_id ?>" />
<span id="hover-test" style="margin:0 0 0 20px;"></span>
</div>
</div></div>
<input id="submitscore" type="submit" value="Submit scores!" onclick="addScore()" />
<u>Test results</u>:<br/><br/>
<div class="test Smaller">
<span style="color:#FF0000">Results will be displayed here</span>
</div>
</form>
答
没有必要使用$("#submitscore").click(function()
在addSote()
方法,因为它呼吁点击按钮
function addScore() {
var show_id = $('#show_id').val();
var user_id = $('#user_id').val();
var score = $('input[name=tvshowrating]:checked').val();
if(!score)
{
alert('PleaseEnter A Score');
}
else
{
$("#flash").show();
$("#flash").fadeIn(400).html('<img src="ajax-loader.gif" />Loading Score...');
$.ajax({
type: "POST",
url: "showscoreajax.php",
data:{
"show_id" : show_id,
"user_id" : user_id,
"score" : score //we are passing the name value in URL
},
cache: false,
success: function(html){
$("#flash").html('Added');
}
});
}
return false;
};
答
删除以下行来自JS的功能:
$("#submitscore").click(function()
{
并关闭它,因为您已通过提交按钮上的单击事件调用它。
答
如有复选框被
if($('input[name=tvshowrating]:checked')[0]) { //will return true if checked element exists }
+0
谢谢你,我会试试看! – CharlieAus 2013-05-10 11:52:15
这样的作品,除了闪光灯股利不显示进度检查你可以检查? – CharlieAus 2013-05-10 11:39:29
@CharlieAus它是什么都没有显示 – 2013-05-10 11:44:24
不,它不是... – CharlieAus 2013-05-10 11:51:33