考虑下面的jQuery代码。它根据用户选择的几个单选按钮设置var,它将该选择的值添加到URL。然后,该URL设置为按钮的数据单击属性。
这一切都很好,如果不是因为函数setcheckoutLocation似乎是在选择radiobutton时触发的。 我期望jQuery attr代码为所选元素设置属性,而不是立即运行它。它只需要在单击按钮本身(#directcheckoutbutton)时运行。
jQuery(document).ready(function() {
jQuery('input').on('change', function() { // On choice selection
var productcode = jQuery('input:checked', '#product-options-wrapper').val(); // Get chosen value code
var quickbuyurl = '<?php echo $_directcheckout; ?>'+productcode; // Add code to end of url
jQuery('#directcheckoutbutton').attr('data-click', setcheckoutLocation(quickbuyurl)); // Set url as onClick for button
});
});
我认为以这种方式调用函数的方式可能会导致浏览器将其作为可执行函数读取,而不是(例如)需要附加到属性的字符串。 但我似乎无法弄明白,我已经尝试将函数设置为单独的var作为字符串,然后附加,但它不起作用。
答案 0 :(得分:2)
适当的解决方案如下,使用您当前的代码段:
jQuery(document).ready(function() {
jQuery('input').on('change', function() { // On choice selection
var productcode = jQuery('input:checked', '#product-options-wrapper').val(); // Get chosen value code
var quickbuyurl = '<?php echo $_directcheckout; ?>'+productcode; // Add code to end of url
jQuery('#directcheckoutbutton').on('click', function(){ setcheckoutLocation(quickbuyurl);}); // Set url as onClick for button
});
});
这样包装的功能将在点击时执行。
编辑:
只是添加一些解释,在原始代码中,您将函数setcheckoutLocation的结果链接到click事件。
哪个可能是null或void,用一个简单的函数(){}
包装它定义函数,但此处尚未执行。
你也可以这样做。
var wrapperFunction = function(){ setCheckoutLocation(yourvariable); }
这样你只需要执行wrapperFunction()即可执行此函数; 或者通过简单地传递wrapperFunction(不带括号)
将其作为参数提供Edit2:看到你做了一个数据点击attr集,它不会做太多,但只是像这样绑定点击工作
答案 1 :(得分:1)
试试这个。
Traceback (most recent call last):
File "C:\Dropbox\Mijn documenten\CiTG\CIE5 - MSc Thesis\Python_TopOpPy_Christopher\testy.py", line 24, in <module>
from mayavi import mlab
File "C:\Python27\lib\site-packages\mayavi\mlab.py", line 27, in <module>
from mayavi.tools.camera import view, roll, yaw, pitch, move
File "C:\Python27\lib\site-packages\mayavi\tools\camera.py", line 25, in <module>
from engine_manager import get_engine
File "C:\Python27\lib\site-packages\mayavi\tools\engine_manager.py", line 12, in <module>
from mayavi.preferences.api import preference_manager
File "C:\Python27\lib\site-packages\mayavi\preferences\api.py", line 4, in <module>
from preference_manager import preference_manager
File "C:\Python27\lib\site-packages\mayavi\preferences\preference_manager.py", line 29, in <module>
from traitsui.api import View, Group, Item
File "C:\Python27\lib\site-packages\traitsui\api.py", line 36, in <module>
from .editors.api import ArrayEditor
File "C:\Python27\lib\site-packages\traitsui\editors\__init__.py", line 23, in <module>
from .api import ArrayEditor
File "C:\Python27\lib\site-packages\traitsui\editors\api.py", line 24, in <module>
from .code_editor import CodeEditor
File "C:\Python27\lib\site-packages\traitsui\editors\code_editor.py", line 36, in <module>
class ToolkitEditorFactory ( EditorFactory ):
File "C:\Python27\lib\site-packages\traitsui\editors\code_editor.py", line 48, in ToolkitEditorFactory
mark_color = Color( 0xECE9D8 )
File "C:\Python27\lib\site-packages\traits\traits.py", line 489, in __call__
return self.maker_function( *args, **metadata )
File "C:\Python27\lib\site-packages\traits\traits.py", line 1203, in Color
return ColorTrait( *args, **metadata )
File "C:\Python27\lib\site-packages\traitsui\toolkit_traits.py", line 7, in ColorTrait
return toolkit().color_trait( *args, **traits )
File "C:\Python27\lib\site-packages\traitsui\toolkit.py", line 125, in toolkit
ETSConfig.toolkit = toolkit_name
File "C:\Python27\lib\site-packages\traits\etsconfig\etsconfig.py", line 213, in _set_toolkit
"already been set to %s" % (toolkit, self._toolkit)
ValueError: cannot set toolkit to wx because it has already been set to qt4
然后使用点击功能:
jQuery(document).ready(function() {
jQuery('input').on('change', function() { // On choice selection
var productcode = jQuery('input:checked', '#product-options-wrapper').val(); // Get chosen value code
var quickbuyurl = '<?php echo $_directcheckout; ?>'+productcode; // Add code to end of url
jQuery('#directcheckoutbutton').attr('data-click', quickbuyurl); // Set url as onClick for button
});
});
这假设您总是使用相同的函数jQuery('#directcheckoutbutton').on("click", function() {
setcheckoutLocation($(this).attr('data-click'));
});
修改
你也可以跳过setcheckoutLocation()
代码并在click函数中处理它:
input