脚本适用于 jsfiddle 但不适用于我的网页 jquery
在@Joseph 的帮助下,我成功创建了这个脚本,它的工作方式完全按照我想要的方式在 jsfiddle 上运行。但是,当我在网页上使用完全相同的脚本时,它不会执行任何操作,它会忽略 if 和 else 内的脚本,并且像普通单选按钮一样工作。我没有收到任何错误消息,并且我正在使用:
http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js
http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.js
我唯一能想到的是使用“this”不起作用?我该如何解决这个问题?
我的脚本可以在以下位置找到:
http://jsfiddle.net/pJgyu/15013/
jQuery(document).ready(function ()
{
$("input[type='radio']").click(function(){
var $knapp = $(this).val();
if(($knapp=='1c') || ($knapp=='2c')|| ($knapp=='3c')) {
this.checked = true;
}else{
$("input[type='radio']."+this.className).not($(this)).each(function(){
this.checked = false;
});
}
});
});
<table>
<tr>
<td>1</td>
<td><input type="radio" class="ljudkalla_1" name="ljudkalla_1" value="1a"></td>
<td><input type="radio" class="ljudkalla_2" name="ljudkalla_1" value="1b"></td>
<td><input type="radio" class="ljudkalla_3" name="ljudkalla_1" value="1c"></td>
</tr>
<tr>
<td>2</td>
<td><input type="radio" class="ljudkalla_1" name="ljudkalla_2" value="2a"></td>
<td><input type="radio" class="ljudkalla_2" name="ljudkalla_2" value="2b"></td>
<td><input type="radio" class="ljudkalla_3" name="ljudkalla_2" value="2c"></td>
</tr>
<tr>
<td>3</td>
<td><input type="radio" class="ljudkalla_1" name="ljudkalla_3" value="3a"></td>
<td><input type="radio" class="ljudkalla_2" name="ljudkalla_3" value="3b"></td>
<td><input type="radio" class="ljudkalla_3" name="ljudkalla_3" value="3c"></td>
</tr>
</table>
With help from @Joseph I have managed to create this script that works exactly the way I want it to on jsfiddle. However, when I use the exact same script on my webpage it does not do anything, it ignores the script inside the if and else and works like a normal radio button would do. I don't get any errror messages and I am using:
http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js
http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.js
The only thing that I can think of is that it does not work using "this"? How would I solve that?
My script can be found at:
http://jsfiddle.net/pJgyu/15013/
jQuery(document).ready(function ()
{
$("input[type='radio']").click(function(){
var $knapp = $(this).val();
if(($knapp=='1c') || ($knapp=='2c')|| ($knapp=='3c')) {
this.checked = true;
}else{
$("input[type='radio']."+this.className).not($(this)).each(function(){
this.checked = false;
});
}
});
});
<table>
<tr>
<td>1</td>
<td><input type="radio" class="ljudkalla_1" name="ljudkalla_1" value="1a"></td>
<td><input type="radio" class="ljudkalla_2" name="ljudkalla_1" value="1b"></td>
<td><input type="radio" class="ljudkalla_3" name="ljudkalla_1" value="1c"></td>
</tr>
<tr>
<td>2</td>
<td><input type="radio" class="ljudkalla_1" name="ljudkalla_2" value="2a"></td>
<td><input type="radio" class="ljudkalla_2" name="ljudkalla_2" value="2b"></td>
<td><input type="radio" class="ljudkalla_3" name="ljudkalla_2" value="2c"></td>
</tr>
<tr>
<td>3</td>
<td><input type="radio" class="ljudkalla_1" name="ljudkalla_3" value="3a"></td>
<td><input type="radio" class="ljudkalla_2" name="ljudkalla_3" value="3b"></td>
<td><input type="radio" class="ljudkalla_3" name="ljudkalla_3" value="3c"></td>
</tr>
</table>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您将 jQuery 包含两次。一种是缩小版本,用<表示代码>min.js。您只需要这些库之一。我建议使用缩小版本来节省带宽。
You are including jQuery twice. One is the minified version, denoted by the
min.js
. You only need one of those libraries. I suggest using the minified version to save bandwidth.这看起来像是 HTML 中的错误,因为从头开始创建 HTML 时它在本地工作。
您还可以尝试使用 Firebug 或 Safari 或 Chrome 的调试器来调试您的版本。在代码中放置一个断点,然后在控制台中手动尝试 jQuery 选择器可以帮助发现大多数问题。
这个有效:
This seems like an error in your HTML, because it works here locally when creating the HTML from scratch.
You can also try to debug your version with Firebug or the debuggers of Safari or Chrome. Put a breakpoint in your code, and trying out the jQuery selectors manually in the console can help to spot most problems.
This one works:
我认为这就是你所追求的,对吗?这可以在您的本地计算机上运行吗?
I think this is what you are after, correct? Does this work on your local machine?