我想检查收音机.以下都不起作用:
[编辑]
$('selector').attr('checked','checked');
$('selector').attr('checked',true);
Run Code Online (Sandbox Code Playgroud)
以下代码中的两个alert()分别显示"1"和"a".我的期望是第二个警告()显示"b".
Opera会检查其浏览器中的第二个单选框,但其元素检查器dragonfly显示DOM未更改.
[结束编辑]
在发布此问题之前,我已阅读过常见问题解答:
帮助将不胜感激!
TIA
xhtml页面和代码如下:
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Weird Behavior</title>
<script type="text/javascript" src="scripts/jquery.js"/>
<script type="text/javascript">
function clickme(){
alert($('input[name="myname"][value="b"]').length);
$('input[name="myname"][value="b"]').attr('checked','checked');
$('#b').attr('checked',true);
alert($('input[name="myname"][checked]').val());
};
</script>
</head>
<body>
<form action="">
<fieldset>
<legend>Why check is not switched?</legend>
<input type="radio" name="myname" value="a" id="a" checked="checked"/>A
<input type="radio" name="myname" value="b" id="b"/>B
</fieldset>
</form>
<p>
<button type="button" onclick="clickme()">click me</button>
</p>
</body>
</html>
Run Code Online (Sandbox Code Playgroud) 我使用命令编译以下代码g++ -std=c++11 t.cpp:
#include <vector>
#include <cstring> //memcpy()
class s
{
char *p;
size_t size;
public:
s(){
size=10;
p=new char[size];
}
s(const s &other){
size=other.size;
p=new char[size];
memcpy(p,other.p,other.size);
}
~s(){ delete [] p; }
};
int main()
{
std::vector<s> ss;
ss.push_back(s());
}
Run Code Online (Sandbox Code Playgroud)
这是gdb日志:
Breakpoint 1, main () at t.cpp:23
23 ss.push_back(s());
s::s (this=0x7fffffffe370) at t.cpp:9
9 size=10;
10 p=new char[size];
11 }
std::vector<s, std::allocator<s> >::push_back(s&&) (this=0x7fffffffe350,
__x=<unknown type in /tmp/a.out, CU 0x0, DIE 0x20d0>)
at /usr/include/c++/4.9/bits/stl_vector.h:932 …Run Code Online (Sandbox Code Playgroud) 我不想在main()中构造ofstream.这是我做的,但它不编译:
#include <fstream>
using namespace std;
class test
{
private:
ofstream &ofs;
public:
test(string FileName);
void save(const string &s);
};
//----------------
test::test(string FileName)
: ofs(ofstream(FileName.c_str(),ios::out))
{
}
//----------------
void test::save(const string &s)
{
ofs << s;
}
//----------------
//Provide file name and to-be-written string as arguments.
int main(int argc,char **argv)
{
test *t=new test(argv[0]);
t->save(argv[1]);
delete t;
}
test.cpp: In constructor ‘test::test(std::string)’:
test.cpp:13: error: invalid initialization of non-const reference of type ‘std::ofstream&’ from a temporary of type ‘std::ofstream’
Run Code Online (Sandbox Code Playgroud)
如何修复代码?