Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/388.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 下载选项表单的两个onClick事件_Javascript_Forms_Events_Onclick - Fatal编程技术网

Javascript 下载选项表单的两个onClick事件

Javascript 下载选项表单的两个onClick事件,javascript,forms,events,onclick,Javascript,Forms,Events,Onclick,我正在尝试制作一个表单,它允许我选择一个单选按钮来更改按钮href的位置,并在javascript弹出窗口中打开它。我所做的href更改的JS是: <script type="text/javascript"> <!-- // return the value of the radio button that is checked // return an empty string if none are checked, or // there are no radio bu

我正在尝试制作一个表单,它允许我选择一个单选按钮来更改按钮href的位置,并在javascript弹出窗口中打开它。我所做的href更改的JS是:

<script type="text/javascript">
<!--
// return the value of the radio button that is checked
// return an empty string if none are checked, or
// there are no radio buttons
function getCheckedValue(radioObj) {
if(!radioObj)
    return "";
var radioLength = radioObj.length;
if(radioLength == undefined)
    if(radioObj.checked)
        return radioObj.value;
    else
        return "";
for(var i = 0; i < radioLength; i++) {
    if(radioObj[i].checked) {
        return radioObj[i].value;
    }
}
return "";
}

// set the radio button with the given value as being checked
// do nothing if there are no radio buttons
// if the given value does not exist, all the radio buttons
// are reset to unchecked
function setCheckedValue(radioObj, newValue) {
if(!radioObj)
    return;
var radioLength = radioObj.length;
if(radioLength == undefined) {
    radioObj.checked = (radioObj.value == newValue.toString());
    return;
}
for(var i = 0; i < radioLength; i++) {
    radioObj[i].checked = false;
    if(radioObj[i].value == newValue.toString()) {
        radioObj[i].checked = true;
    }
}
}
//-->
</script>
 <form name="radioExampleForm" method="get" action="" onsubmit="return false;">
<p><label for="number0"><input type="radio" value="popups/download1.html" name="number"     id="number0"> Free</label>
&nbsp;<label for="number1"><input type="radio" value="popups/download2.html" name="number" id="number1">Premium</label>

<p>
<input type="button" onclick="return hs.htmlExpand(this, { objectType: 'iframe' } ); window.location.href = (getCheckedValue(document.forms['radioExampleForm'].elements['number']));" value="Download" class="button">
</form>

弹出窗口的JS来自Highslide JS(Highslide.com)

我的代码是:

<script type="text/javascript">
<!--
// return the value of the radio button that is checked
// return an empty string if none are checked, or
// there are no radio buttons
function getCheckedValue(radioObj) {
if(!radioObj)
    return "";
var radioLength = radioObj.length;
if(radioLength == undefined)
    if(radioObj.checked)
        return radioObj.value;
    else
        return "";
for(var i = 0; i < radioLength; i++) {
    if(radioObj[i].checked) {
        return radioObj[i].value;
    }
}
return "";
}

// set the radio button with the given value as being checked
// do nothing if there are no radio buttons
// if the given value does not exist, all the radio buttons
// are reset to unchecked
function setCheckedValue(radioObj, newValue) {
if(!radioObj)
    return;
var radioLength = radioObj.length;
if(radioLength == undefined) {
    radioObj.checked = (radioObj.value == newValue.toString());
    return;
}
for(var i = 0; i < radioLength; i++) {
    radioObj[i].checked = false;
    if(radioObj[i].value == newValue.toString()) {
        radioObj[i].checked = true;
    }
}
}
//-->
</script>
 <form name="radioExampleForm" method="get" action="" onsubmit="return false;">
<p><label for="number0"><input type="radio" value="popups/download1.html" name="number"     id="number0"> Free</label>
&nbsp;<label for="number1"><input type="radio" value="popups/download2.html" name="number" id="number1">Premium</label>

<p>
<input type="button" onclick="return hs.htmlExpand(this, { objectType: 'iframe' } ); window.location.href = (getCheckedValue(document.forms['radioExampleForm'].elements['number']));" value="Download" class="button">
</form>

免费的
保险费


有没有可能把这些结合起来,如果有,我做错了什么?当我合并它们时,这两个JS都不起作用。

Highslide允许您通过其配置对象的
src
属性覆盖iframe src:

hs.htmlExpand(此{
objectType:“iframe”,
src:'mypage.html'
});
正如Joshua在当前代码中指出的:
return hs.htmlExpand..
将阻止第二条语句(
window.location..
)的执行。即使要执行,它所能做的就是将当前浏览器窗口的位置更改为目标URL

解决方案如下所示:



自由的
保险费


您将返回第一个事件。我不确定,但我认为这会阻止第二个跑动。尝试用一个返回值将这两个函数包装在一个函数中并调用它。这给了我比以前多一点的运气,但现在无论选择哪个单选按钮,它都只下载1。我不知道我做了什么,但我现在已经开始工作了。谢谢你的帮助!不客气。我创建了一个jsfiddle()来演示。您的控制台应该记录正确的URL。