我有一个脚本,可以切换选择表/表单中的所有复选框-但我需要修改为仅切换/选中未禁用的复选框。非常感谢您的帮助。
<?php
namespace Tests\Browser;
use Facebook\WebDriver\Chrome\ChromeOptions;
use Facebook\WebDriver\Remote\RemoteWebDriver;
use Facebook\WebDriver\Remote\DesiredCapabilities;
use Tests\DuskTestCase;
use Laravel\Dusk\Browser;
use Illuminate\Foundation\Testing\DatabaseMigrations;
class ExampleTest extends DuskTestCase
{
private $ua = 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36';
/**
* A Dusk test example.
*
* @return void
*/
public function testExample()
{
$this->browse(function (Browser $browser ) {
$browser->visit('https://www.whatismybrowser.com/');
dd("Stop");
});
}
protected function driver()
{
$ua = $this->ua;
$capabilities = DesiredCapabilities::chrome();
$options = (new ChromeOptions)->addArguments([
'--disable-gpu',
// '--headless',
'--force-device-scale-factor=0.8',
'--window-size=1920,1080',
]);
$options->setExperimentalOption('mobileEmulation', ['userAgent' => $ua]);
return RemoteWebDriver::create(
'http://localhost:9515', $options->toCapabilities()
);
}
}
答案 0 :(得分:2)
您可以使用querySelectorAll()
定位所有允许任何有效CSS selector的 enabled 输入。您还可以使用forEach()
遍历更简洁的元素:
function toggle(source) {
var checkboxes = document.querySelectorAll('input[name="chkInvID"]:enabled');
[].slice.call(checkboxes).forEach(function(chk){
chk.checked = source.checked;
});
}
document.querySelector('input[name="source"]').addEventListener('change', function(){toggle(this)});
<input name="source" type="checkbox"/>source<br>
<input name="chkInvID" type="checkbox" />
<input name="chkInvID" type="checkbox" disabled/>
<input name="chkInvID" type="checkbox" />
答案 1 :(得分:1)
您可以通过一个简单的条件来实现:
function toggle(source) {
checkboxes = document.getElementsByName('chkInvID');
for(var i=0, n=checkboxes.length;i<n;i++) {
if(!checkboxes[i].disabled){
checkboxes[i].checked = source.checked;
}
}
}