[英]Need to change onClick value of a button to another value using select
I am a little lost at this point in my project. 在我的项目中,我有点迷失了。 I work for a company that provides translation services.
我在一家提供翻译服务的公司工作。 Well, I am making a simple calculator to calculate the cost of a translation based on the number of words in the entire project.
好吧,我正在制作一个简单的计算器,根据整个项目中的单词数计算翻译成本。 It also changes rate based on the amount of words entered.
它还会根据输入的单词数量更改费率。 I have that part done.
我完成了这一部分。 I wrote this and it works:
我写了这个并且它有效:
function wordcountprice(){
var price, final,
a=document.getElementById('words'),
wc= parseFloat(a.value) || 0;
if(wc< 10000) price= .29;
else price= wc<20000? .26: .24;
final= (wc*price).toFixed(2);
document.getElementById('estPrice').innerHTML = 'Your estimated price is: $' + final;}
At this point we have 6 types of translation work. 此时我们有6种类型的翻译工作。 Five of them would use that function and the last one would use a basic function with a fixed price.
其中五个将使用该功能,最后一个将使用固定价格的基本功能。 I was thinking I would use a drop down select box to change functions.
我以为我会使用下拉选择框来更改功能。
I am calling the above function in my HTML like this: 我在我的HTML中调用上面的函数,如下所示:
<input type="text" id="words"> <input type="button" id="button" value="Calculate" onClick="wordcountprice();"> <br /><b id='estPrice'>Estimated Price</b>
I have tried several things and none of them have worked. 我尝试了几件事,但没有一件有效。 I assume that the best way to go about this is jQuery's change() or select() function.
我假设最好的方法是使用jQuery的change()或select()函数。 I have tried making a new function with jQuery like this one:
我试过像这样用jQuery创建一个新函数:
$(function() {
$('#type').change(function() {
var type = $(this).val();
$('#button').val(type);
}); });
That only changes the value of the button and not the onClick event. 这只会更改按钮的值而不是onClick事件。 I do not know how, or if, it is possible to change the onClick event like that.
我不知道如何或者是否可以像这样更改onClick事件。 I don't even know if that is the most efficient method to do what I want to do.
我甚至不知道这是否是做我想做的最有效的方法。 I called that function when I was trying it with this HTML:
当我使用这个HTML尝试时,我调用了该函数:
<select id='type'><option value='1'>A</option>
<option value='2'>B</option>
<option value='3'>C</option>
<option value='4'>D</option></select>
I am a noob with jQuery and haven't been programming for a while so I am a little rusty. 我是jQuery的菜鸟,并且一段时间没有编程,所以我有点生疏。
Thanks for the help in advance. 我在这里先向您的帮助表示感谢。
There are a couple options. 有几种选择。
You could bind a function to the button's click event: $('#button').click(myFunction)
and either have myFunction check the value of $('#type')
or keep track of type internally in the javascript when it changes. 你可以将一个函数绑定到按钮的click事件:
$('#button').click(myFunction)
并让myFunction检查$('#type')
的值,或者在javascript更改时在内部跟踪类型。 Then you can act conditionally within myFunction based on type to get the reaction you want 然后你可以根据类型在myFunction中有条件地采取行动,以获得你想要的反应
You can bind a function to the button for the intial type, then in the change handler, first call $('#button').unbind('click')
, and then bind a new event (again, $('#button').click(myNewFunction)
where myNewFunction is chosen based upon type 你可以将一个函数绑定到初始类型的按钮,然后在更改处理程序中,首先调用
$('#button').unbind('click')
,然后绑定一个新事件(再次, $('#button').click(myNewFunction)
根据类型选择myNewFunction
I think you want to do something like this: 我想你想做这样的事情:
$('#button').attr("onClick", "wordcountprice('"+type+"');" );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.