[英]how to check a value is being properly selected (or not) by javascript?
I'm trying to add a value from a radio button form into my DB but my javascript doesn't return any errors and it's not working. 我正在尝试从单选按钮表单中添加一个值到我的数据库中,但是我的JavaScript没有返回任何错误,并且无法正常工作。 I think my selector might not be working but how do I check that ? 我认为我的选择器可能不起作用,但如何检查呢? What's wrong with my function ? 我的功能出了什么问题?
JS 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 的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>
There is no need to use $("#submitscore").click(function()
in the addSote()
method since it called on click on the button 无需在addSote()
方法中使用$("#submitscore").click(function()
,因为它是在单击按钮时调用的
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;
};
Remove the following lines from the JS function: 从JS函数中删除以下几行:
$("#submitscore").click(function()
{
and also closing of it as you are already call it by click event on the submit button. 并关闭它,因为您已经通过提交按钮上的click事件调用了它。
您可以检查是否选中了任何复选框
if( $('input[name=tvshowrating]:checked')[0]) { //will return true if checked element exists }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.