我的页面上有一个带有两个输入字段和一个提交按钮的表单,我希望在两个输入字段都有值的情况下禁用 "提交"按钮.当且仅当两个字段中都有值输入时,该按钮才可以单击.
如何用js和jQuery实现这个?
这是我的页面:
<html>
<body>
<form method=post>
<input type=text id='first_name'>
<input type=text id='second_name'>
<input type=submit value=Submit>
</form>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
我想同时拥有js和jQuery解决方案
我在CoffeeScript/NodeJS中看到一些关于EventEmitters和处理程序的非常奇怪的行为.我把一个展示问题的小样本放在一起......
基本上我在事件处理方面有一些间接性,但除非我将第一个事件处理程序包装在lambda中,否则我似乎无法使它工作,我想知道为什么/如果我能做些什么来使这个工作.test1()从我的思维方式来看,基本上应该具有与之相同的行为test3().test2()包含只是为了表明第二级事件处理工作!
events = require "events"
class ExampleEmitter extends events.EventEmitter
constructor: () ->
go1: () ->
console.log("fire 1")
@emit("test1", "there")
go2: () ->
console.log("fire 2")
@emit("test2", "there")
class ExampleHandler
constructor: () ->
handle: (x) -> console.log("hey", x)
test1 = () ->
handler = new ExampleHandler()
emitter1 = new ExampleEmitter()
emitter2 = new ExampleEmitter()
emitter1.on "test1", emitter2.go2
emitter2.on "test2", handler.handle #this doesn't fire :(
emitter1.go1()
test2 = () ->
handler = new ExampleHandler()
emitter1 = …Run Code Online (Sandbox Code Playgroud) events event-handling javascript-events node.js coffeescript
我有两个要素:
<input type="text" name="pmTo" id="pmTo" onkeyup="offerSuggs('none');" onfocus="showSelect();" onblur="hideSelect();" />
Run Code Online (Sandbox Code Playgroud)
和一个动态创建的(这是自动建议程序的所有部分):
$suggString .= '<div name="'.$value.'" id="'.$value.'" class="suggMouseOver" onmouseover="highlightDivs(this);" onmouseout="blurDivs(this);" onclick="fillInput(this);">'.$value.'</div>';
Run Code Online (Sandbox Code Playgroud)
好的,然后有一些事件函数对应于输入元素的onblur,然后是div元素的onclick:
function hideSelect() {
offerSuggs('checkFinal');
document.getElementById('nameSugg').style.display="none";
}
function fillInput(elemName) {
document.getElementById('pmTo').value=elemName.id;
}
Run Code Online (Sandbox Code Playgroud)
编辑:如何让onclick在他们点击的元素上正确触发而没有onblur事件隐藏div首先使onclick无意义?但是,当文本框失去焦点时,我仍然希望保留建议div消失的功能.谢谢.
我在这里看到了一个javascript MVC文章,模型定义为:
var ListModel = function (items) {
this._items = items;
this._selectedIndex = -1;
this.itemAdded = new Event(this);
this.itemRemoved = new Event(this);
this.selectedIndexChanged = new Event(this);
};
ListModel.prototype = {
getItems : function () {
return [].concat(this._items);
},
addItem : function (item) {
this._items.push(item);
this.itemAdded.notify({item: item});
},
removeItemAt : function (index) {
var item = this._items[index];
this._items.splice(index, 1);
this.itemRemoved.notify({item: item});
if (index == this._selectedIndex)
this.setSelectedIndex(-1);
},
getSelectedIndex : function () {
return this._selectedIndex;
},
setSelectedIndex : function (index) …Run Code Online (Sandbox Code Playgroud) javascript model-view-controller javascript-events javascript-framework
通常JavaScript绑定DOM元素上的事件.但我想知道用户点击了哪个词.对我来说熟悉的方法就是像这里一样包装每一个字:
<a href="javascript:onClick()">And</a> <a href="javascript:onClick()">now</a> <a href="javascript:onClick()">the</a>
<a href="javascript:onClick()">kung</a> <a href="javascript:onClick()">pao</a> <a href="javascript:onClick()">chicken</a>.
Run Code Online (Sandbox Code Playgroud)
我认为这是一个冗余的代码,是否可以使代码更简洁?谢谢
当我尝试实现以下代码时:
$('.touchelement').bind({'touchstart':function(e){
console.info(e.touches.item(0));
}});
Run Code Online (Sandbox Code Playgroud)
它将输出显示为undefined.不仅在touchstart中,而且对于像touchmove,touchend这样的其他事件,它显示相同的结果.
是否有任何使用jquery绑定touchstart事件的替代方法.
在下面的代码中,我试图使它在选择(聚焦)输入时,相关字段集中的"formhead"div改变其背景颜色,并且在模糊时它会改变回来.我想通过在焦点上向同一个div添加一个类,并在模糊时删除该类来实现此目的.jQuery最接近的概念与我正在尝试做的最相似,但是不合适,因为它只会针对父div.是否有其他东西可以使用该类定位最近的div,而不会影响其他字段集中的其他类?或者我是否必须更加具体并且标识形状等?
<fieldset>
<div class="formhead">Heading Title 1</div>
<div class="fieldleft">
<label for="specificinput">Input Title</label>
<input type="text" class="forminput" id="specificinput">
</div>
<div class="fieldleft">
<label for="specificinput">Input Title</label>
<input type="text" class="forminput" id="specificinput">
</div>
</div>
</fieldset>
<fieldset>
<div class="formhead">Heading Title 2</div>
<div class="fieldleft">
<label for="specificinput">Input Title</label>
<input type="text" class="forminput" id="specificinput">
</div>
<div class="fieldleft">
<label for="specificinput">Input Title</label>
<input type="text" class="forminput" id="specificinput">
</div>
</div>
</fieldset>
Run Code Online (Sandbox Code Playgroud)
和jQuery:
<script type="text/javascript">
$(document).ready(function() {
$('input').focus(function( ){
$(this).closest('.formhead').addClass('bluebg');
});
$('input').focus(function( ){
$(this).closest('.formhead').removeClass('bluebg');
});
});
</script>
Run Code Online (Sandbox Code Playgroud) 我想在div徘徊时显示一个锚点链接.这工作正常,但当我将鼠标悬停在链接本身上时,它会闪烁,此时它应该停留.我在锚链接上尝试了一个常规的悬停功能,但是没有用......这是我的js:
$('.div_mayKnowUser').live("mouseover", function(){
$(this).find('.a_ignoreUser').fadeIn();
});
$('.div_mayKnowUser').live("mouseout", function(){
$(this).find('.a_ignoreUser').fadeOut();
});
和我的HTML:
<div class="div_mayKnowUser">
<a href="" class="a_ignoreUser">a link</a>
</div>
Run Code Online (Sandbox Code Playgroud)
谢谢!
我在其中一个问题上找到了这个代码示例;
window.onerror = function (msg, url, linenumber) {
//make ajax call with all error details and log error directly into the elmah database
//show freindly error msg here to user
//hide error from browser
return true;
}
Run Code Online (Sandbox Code Playgroud)
什么"从浏览器中隐藏的错误"的手段,我该怎么办呢?
我不知道存在这样的事情,我尝试使用try-catch块.发生错误后,我意识到浏览器仍然显示错误.如何从浏览器隐藏错误
UPDATE
我尝试了下面的代码并把它放在我所有页面的head部分中,但是注意到了;
<script type="text/javascript">
(function () {
window.onerror = function myErrorHandler(errorMsg, url, lineNumber) {
alert(errorMsg);
// Just let default handler run.
return true;
}
})();
</script>
Run Code Online (Sandbox Code Playgroud) 我有一些代码,每次onmouseclick工作,并在我相应设置时不断onmousemove.我正在寻找一种方法来结合两者(即像点击和拖动),我认为这将是一个简单的任务,但找不到任何简单的解释.我得到的最接近的是拖放教程.
在onmouseclick事件被触发时,我是否必须调用onmousemove事件?或者有什么非常简单的我完全忽视?
干杯
javascript ×8
jquery ×5
onblur ×2
client-side ×1
coffeescript ×1
events ×1
iphone ×1
jquery-ui ×1
node.js ×1
onclick ×1
onfocus ×1